package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.ShopList;
import com.myapp.ruyixing.model.ShopList.Shop;

public class ShopListParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {
		if (getErrno(jsonObject) != NO_ERROR) {
			return null;
		}

		ShopList shopList = new ShopList();
		shopList.list = new ArrayList<ShopList.Shop>();

		JSONObject data = jsonObject.optJSONObject("data");
		shopList.pn = data.optInt("pn");
		shopList.rn = data.optInt("rn");
		shopList.total = data.optInt("total");

		JSONArray list = data.optJSONArray("list");

		for (int i = 0; list != null && i < list.length(); i++) {
			JSONObject object = list.optJSONObject(i);
			if (object != null) {

				Shop shop = new Shop();
				shop.averagecost = object.optString("averagecost", "");
				shop.distance = object.optLong("distance", 0);
				shop.id = object.optString("id", "");
				shop.name = object.optString("name", "");
				shop.keywords = object.optString("keywords", "");
				shop.picurl = object.optString("picurl", "");
				shop.score = object.optInt("score");
				shopList.list.add(shop);
			}
		}

		return shopList;

	}
}
